package com.aube.plugin.figure;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.aube.plugin.origin.R;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.DynamicLoaderHelper;
import com.huyn.baseframework.dynamicload.IPositionListener;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.model.Character;
import com.huyn.baseframework.utils.SysUtil;
import com.ryg.dynamicload.internal.DLIntent;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huyaonan on 16/5/23.
 */
public class GuestOneLineView extends DLLayout implements IFigureView {

    private LinearLayout mLayoutGroup;
    private List<IPositionListener> mAnimItems = new ArrayList<>();

    public GuestOneLineView(Context context) {
        this(context, null);
    }
    public GuestOneLineView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public GuestOneLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initViews() {
        inflate(R.layout.widget_guest, this, true);
        mLayoutGroup = (LinearLayout) findViewById(R.id.guest_widget_group);
    }

    @Override
    public void fillData(Object o) {
        if(o == null || !(o instanceof List))
            return;
        int width = SysUtil.getAbsoluteScreenWidth(context);
        int space = getResources().getDimensionPixelOffset(R.dimen.widget_margin);
        int itemW = (width-6*space)/4;

        List list = (List) o;
        int size = list.size();
        for(int i=0; i<size; i++) {
            final Character character = (Character) list.get(i);

            GuestItem item = (GuestItem) DynamicLoaderHelper.loadTargetLayout(context, getDLPluginPackage(), GuestItem.class.getName());
            LinearLayout.LayoutParams prams = new LinearLayout.LayoutParams(itemW, ViewGroup.LayoutParams.WRAP_CONTENT);

            mLayoutGroup.addView(item, prams);
            item.resizeImgSize(itemW);
            item.fillData(character);
            item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mBridge != null && character.canClick()) {
                        DLIntent intent = new DLIntent("com.aube.plugin.origin", "com.aube.plugin.figure.CharacterActivity");
                        intent.putExtra(CharacterActivity.ACTOR, character);
                        mBridge.openPluginActivity(intent);
                    }
                }
            });

            item.setAlpha(0f);
            mAnimItems.add(item);

            if (i != size - 1)
                mLayoutGroup.addView(new View(context), new LinearLayout.LayoutParams(space, space));
        }
    }

    public void anim(int animDelay) {
        int i=0;
        for (IPositionListener item : mAnimItems) {
            item.notifyAnim(animDelay + i*200);
            i++;
        }
    }

    @Override
    public void notifyAnim(int animDelay) {

    }

    @Override
    public void registToManager(IPositionManager iPositionManager) {

    }

    @Override
    public void unreistFromManager(IPositionManager iPositionManager) {

    }

    @Override
    public int[] getLocations() {
        return new int[0];
    }
}
